

    ### Weight calculators
    
    Coarsen_eX_Subclass = function(eX,Z,K){
    # Input:
    #   eX: estimated propensity scores
    #   Z:  treatment indicator
    #   K:  number of subclasses
    # Output
    #   eX.coarse: coarsened hateX using subclassification
        
      
      if(is.na(K)) return(eX)
      
      ##To make the breakpoints unique, we use the following code to calculate q
        q    = unique(quantile(eX,prob=(0:K)/K,na.rm=TRUE))
        q[1] = q[1]/2; q[length(q)] = 1
        K = length(q)-1
        
        ki   = as.numeric(cut(eX,q))
        
        pk = sapply(1:K,function(k){
            return(mean(Z[ki==k]))
        })
        
        eX.coarse = pk[ki]
        
        return(eX.coarse)
        
    }

    

    
    
    